package com.cskaoyan.connectionpool;

import org.apache.commons.dbcp.BasicDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 创建日期: 2022/03/23 11:48
 *
 * @author ciggar
 */
public class DBCPUtils {

    // 数据源
    static DataSource dataSource;

    static {

        Properties properties = new Properties();
        try {
            FileInputStream fileInputStream = new FileInputStream("dbcp.properties");
            properties.load(fileInputStream);

            dataSource = BasicDataSourceFactory.createDataSource(properties);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    // 获取连接
    public static Connection getConnection(){
        Connection connection = null;
        try {

            connection = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return connection;

    }
}
